package com.zhdl.modules.prefiresys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.project.common.service.impl.BaseServiceImpl;
import com.zhdl.modules.prefiresys.entity.Detector;
import com.zhdl.modules.prefiresys.entity.DetectorAreaInfo;
import com.zhdl.modules.prefiresys.entity.QDetectorAreaInfo;
import com.zhdl.modules.prefiresys.repository.DetectorRepository;
import com.zhdl.modules.prefiresys.service.DetectorAreaInfoService;
import com.zhdl.modules.prefiresys.service.DetectorService;
import com.zhdl.modules.prefiresys.service.dto.BaseQueryCriteria;
import com.zhdl.modules.prefiresys.web.request.DetectorAreaRequest;
import com.zhdl.modules.prefiresys.web.request.DetectorInsertRequest;
import com.zhdl.modules.prefiresys.web.request.DetectorUpdateRequest;
import com.zhdl.modules.prefiresys.web.response.AllDetectorListResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class DetectorServiceImpl extends BaseServiceImpl<String, Detector, DetectorRepository> implements DetectorService {

    @Autowired
    DetectorAreaInfoService detectorAreaInfoService;

    /**
     * 新增探测器设备信息
     *
     * @param request
     * @return
     */
    @Override
    public Detector addDetector(DetectorInsertRequest request) {
        Detector entity = new Detector();
        BeanUtil.copyProperties(request, entity, CopyOptions.create().setIgnoreNullValue(true));
        DetectorAreaInfo areaInfo = new DetectorAreaInfo();
        entity.setId(IdUtil.fastSimpleUUID());
        Detector newDto = insert(entity);

        for (int i = 0; i < request.getAreaInfo().length; i++) {
            DetectorAreaRequest area = request.getAreaInfo()[i];
            areaInfo.setId(IdUtil.fastSimpleUUID());
            areaInfo.setAreaNo((byte) (i + 1));
            areaInfo.setDetectorId(newDto.getId());
            areaInfo.setDetectorAddr(newDto.getDetectorAddr());
            areaInfo.setAreaName(area.getAreaName());
            areaInfo.setStatus(area.isStatus());
            areaInfo.setAlarmStatusAt(DateUtil.now());
            detectorAreaInfoService.insert(areaInfo);
        }

        return newDto;
    }

    /**
     * 更新探测器设备信息
     *
     * @param request
     * @return
     */
    @Override
    public Detector updateDetector(DetectorUpdateRequest request) {
        Detector entity = new Detector();
        BeanUtil.copyProperties(request, entity, CopyOptions.create().setIgnoreNullValue(true));
        entity = update(entity, entity.getId());

        for (int i = 0; i < request.getAreaInfo().length; i++) {
            DetectorAreaRequest area = request.getAreaInfo()[i];
//            detectorAreaInfoService.updateAreaByNo(entity.getId(), (byte) (i + 1), area.getAreaName(), area.isStatus());
            detectorAreaInfoService.updateAreaByNo(entity.getId(), (byte) (i + 1), area.getAreaName(), area.isStatus());
        }

        return entity;

    }

    /**
     * 删除设备信息,同时删除防区
     *
     * @param id 设备id
     * @return 状态码 1
     */
    @Override
    public int deleteDetector(String id) {
        deleteById(id);

        detectorAreaInfoService.deleteByDetectorId(id);

        return 1;
    }

    /**
     * 获取设备列表，带防区名称列表
     *
     * @return
     */
    @Override
    public List<AllDetectorListResp> getAllDetectorlist() {

        BaseQueryCriteria bc = new BaseQueryCriteria();
        List<Detector> list = findList(bc);

        QDetectorAreaInfo qDetectorAreaInfo = QDetectorAreaInfo.detectorAreaInfo;
        List<AllDetectorListResp> outData = new ArrayList<>();
        for (Detector item : list) {
            AllDetectorListResp detRes = new AllDetectorListResp();
            BeanUtil.copyProperties(item, detRes, CopyOptions.create().setIgnoreNullValue(true));

            List<String> areaName = new ArrayList<>();
            List<DetectorAreaInfo> areaList = jpaQueryFactory().selectFrom(qDetectorAreaInfo)
                    .where(qDetectorAreaInfo.detectorId.eq(item.getId()))
                    .orderBy(qDetectorAreaInfo.areaNo.asc())
                    .fetch();
            areaList.forEach(area -> {
                areaName.add(area.getAreaName());
            });
            detRes.setAreaName(areaName);
            outData.add(detRes);
        }

        return outData;
    }
}
